package com.springboot.blog.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.blog.dto.LayResponse;
import com.springboot.blog.entity.Category;
import com.springboot.blog.interfaces.ToolInterface;
import com.springboot.blog.service.ICategoryService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
class AdminCategoryController {
    @Autowired
    ICategoryService categoryService;
    String PATH = "admin/category/";

    @RequiresPermissions("category:list")
    @GetMapping(value = {"/admin/category/index", "/admin/category"})
    public String index() {
        return PATH + "index";
    }

    @RequiresPermissions("category:list")
    @PostMapping(value = {"/admin/category/tree"})
    @ResponseBody
    public LayResponse tree() {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        List<Category> categories = categoryService.list(wrapper.lambda().orderByAsc(Category::getSorts));
        return new LayResponse().success("成功!").data(categories).setCount(9L);
    }

    @RequiresPermissions("category:delete")
    @PostMapping(value = {"/admin/category/delete"})
    @ResponseBody
    public LayResponse delete(String id) {
        boolean result = categoryService.removeById(id);
        LayResponse layResponse = new LayResponse();
        if (result) {
            return layResponse.success();
        }
        return layResponse.error();
    }

    @RequiresPermissions("category:delete")
    @PostMapping(value = {"/admin/category/deleteAll"})
    @ResponseBody
    public LayResponse deleteAll(@RequestParam("ids[]") List<Integer> ids) {
        boolean result = categoryService.removeByIds(ids);
        LayResponse layResponse = new LayResponse();
        if (result) {
            return layResponse.success();
        }
        return layResponse.error();
    }

    @RequiresPermissions("category:edit")
    @PostMapping(value = {"/admin/category/ajax/{id}"})
    @ResponseBody
    public LayResponse ajax(Category category) {
        boolean b = categoryService.updateById(category);
        LayResponse layResponse = new LayResponse();
        if (b) {
            return layResponse.success();
        }
        return layResponse.error();
    }

    @RequiresPermissions("category:add")
    @GetMapping(value = {"/admin/category/add"})
    public String add(Model model) {
        List<Category> list = categoryService.list(new QueryWrapper<Category>().lambda().orderByAsc(Category::getSorts));
        List<Category> list_sort= new ArrayList<>();
        unlimitedSort(list,list_sort,0,0);
        model.addAttribute("categories",list_sort);
        return PATH + "add";
    }

    @RequiresPermissions("category:add")
    @PostMapping(value = {"/admin/category/add"})
    @ResponseBody
    public LayResponse add(@Validated(ToolInterface.insert.class) Category category) {
        categoryService.save(category);
        return new LayResponse().success();
    }
    private void unlimitedSort(List<Category> list, List<Category> list_sort, int pid, int level) {
        for (Category item : list) {
            if (item.getPid() == pid) {
                item.setTitle(StringUtils.repeat("　　", level) + "├" + item.getTitle());
                list_sort.add(item);
                unlimitedSort(list, list_sort, item.getId(), level + 1);
            }
        }
    }

    @RequiresPermissions("category:edit")
    @GetMapping(value = {"/admin/category/edit"})
    public String edit(Model model,@RequestParam Integer id) {
        List<Category> list = categoryService.list(new QueryWrapper<Category>().lambda().orderByAsc(Category::getSorts));
        List<Category> list_sort= new ArrayList<>();
        unlimitedSort(list,list_sort,0,0);
        Category category = categoryService.getById(id);
        model.addAttribute("categories",list_sort);
        model.addAttribute("category",category);
        return PATH + "edit";
    }

    @RequiresPermissions("category:edit")
    @PostMapping(value = {"/admin/category/edit"})
    @ResponseBody
    public LayResponse edit(@Validated(ToolInterface.update.class) Category category) {
        categoryService.updateById(category);
        return new LayResponse().success();
    }
}
